package chart5;


import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;

/**
 *
 * @author Егор
 */
public class Chart5 {

    /**
     *
     * @param args pathes of datafiles.
     */

    public static void main(String[] args) {
       

        //Checking for write number of arguments

        if ( args.length == 0 )
        {
            System.out.println("You've passed no arguments. Please pass pathes of your data files");
            return;
        }

        // Finding the earliest date and the last.

        Date mindate = new Date(0);
        Date maxdate = new Date(0);


        for (int i = 0; i < args.length; ++i)
        {
            String s = args[i];
            try
            {
          BufferedReader in = new BufferedReader(new FileReader(s));  
          String str;
          while ((str = in.readLine()) != null)
          {
             //"2011-05-22 14:38:39","26" 
             int year = Integer.parseInt(str.substring(1, 5));
             int month = Integer.parseInt(str.substring(6,8));
             int day = Integer.parseInt(str.substring(9,11));
             
             int hour = Integer.parseInt(str.substring(12,14));
             int minute = Integer.parseInt(str.substring(15,17));
             int sec = Integer.parseInt(str.substring(18,20));
             
             int number = Integer.parseInt(str.substring(23,str.length()-1));
             
             
            Date date = new Date(year,month,day,hour,minute,sec);
            
            if (mindate.getTime() == 0 )
            {
                mindate = new Date(date.getTime());
                maxdate = new Date(date.getTime());
            }
            
            if (mindate.after(date)) mindate = date;
            if (maxdate.before(date)) maxdate = date;
            
            
             
            /* System.out.print("\n");
             System.out.print(str+" "+Integer.toString(number)+" "+
                     Integer.toString(year)+" "+
                             Integer.toString(month)+" "+
                     Integer.toString(day)+" "+
                     Integer.toString(hour)+" "+
                     Integer.toString(minute)+" "+
                     Integer.toString(sec)+" ");
             System.out.print(date); */
          }
          in.close();
            }
            catch (Exception e)
            {
                System.out.print("\nProblems working with file: "+e.getMessage());
                return;
            }
        }


      // Creating dataset and storing data in it

       DataSet dataset = new DataSet(mindate,maxdate);

       for (int i = 0; i < args.length; ++i)
        {
            String s = args[i];
            try
            {
          BufferedReader in = new BufferedReader(new FileReader(s));
          String str;
          while ((str = in.readLine()) != null)
          {
              //"2011-05-22 14:38:39","26"

             int year = Integer.parseInt(str.substring(1, 5));
             int month = Integer.parseInt(str.substring(6,8));
             int day = Integer.parseInt(str.substring(9,11));

             int hour = Integer.parseInt(str.substring(12,14));
             int minute = Integer.parseInt(str.substring(15,17));
             int sec = Integer.parseInt(str.substring(18,20));

             long number = Integer.parseInt(str.substring(23,str.length()-1));


            Date date = new Date(year,month,day,hour,minute,sec);

            dataset.addRecord(date, number);

/*
             System.out.print("\n");
             System.out.print(str+" "+
                     Integer.toString(year)+" "+
                             Integer.toString(month)+" "+
                     Integer.toString(day)+" "+
                     Integer.toString(hour)+" "+
                     Integer.toString(minute)+" "+
                     Integer.toString(sec)+" ");
             System.out.print(date); */
          }
          in.close();
            }
            catch (Exception e)
            {
                System.out.print("\nProblems working with file: "+e.getMessage());
                return;
            }
        }

    //  dataset.printData();
        dataset.makeChart();
    }
}
